//
//  VisitorLoginView.swift
//  weibo
//
//  Created by 谭仁川 on 15/11/27.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

@objc protocol VisitorLoginViewDelegate: NSObjectProtocol{
   optional func userWilllogin()
    
    func userWillRegister()
}
class VisitorLoginView: UIView {
    
    weak var visitorViewDelegate:VisitorLoginViewDelegate?
    
    func setWithInfo(tipText: String,imageName: String?){
        tipLabel.text = tipText
        
        if let name = imageName{
        
            circleView.image = UIImage(named: name)
        
            iconView.hidden = true
            
            bringSubviewToFront(circleView)
        } else {
        
            startAnimation()
        }
    
    }
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        
        anim.duration = 20.0
        
        anim.toValue = 2 * M_PI
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    private func setupUI(){
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        
        //按钮的约束
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        

        //添加遮罩视图的约束
        //OC中有多个枚举选项 使用 按位或  swift-> 给定数组
       addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options:[] , metrics: nil, views: ["maskView": backView]))
        
       addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-(-35)-[regBtn]", options:[] , metrics: nil, views: ["maskView": backView,"regBtn": registerBtn]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        loginBtn.addTarget(self, action: "loginDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerDidClick", forControlEvents: .TouchUpInside)
        
    }
    @objc private func loginDidClick(){
        visitorViewDelegate?.userWilllogin?()
    
    }
    @objc private func registerDidClick(){
        visitorViewDelegate?.userWillRegister()
        
    }
    
    
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
    
    private lazy var loginBtn: UIButton = UIButton(title: "登录", backImage: "common_button_white_disable", color: UIColor.darkGrayColor(), fontSize: 15)
    
    
    private lazy var registerBtn: UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color: UIColor.orangeColor(), fontSize: 15)
    
      

}
